package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Environment
import android.os.Handler
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.repository.AddDriveActivityRepository
import com.cheyun.netsalev3.view.mars.choosecar.MarsChooseCarActivity
import com.cheyun.netsalev3.widget.MyContentDialog
import com.cheyun.netsalev3.widget.MyContentDialogLayout
import com.cheyun.netsalev3.widget.PopDialog
import java.io.Serializable
import android.print.PrintAttributes
import android.print.PrintManager
import com.cheyun.netsalev3.adapter.MyPrintAdapter
import com.cheyun.netsalev3.utils.DownloadUtil
import java.io.File
import android.os.Looper
import android.view.Gravity
import android.widget.DatePicker
import android.widget.ImageView
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.basedata.BaseDataParam
import com.cheyun.netsalev3.bean.basedata.Expchar
import com.cheyun.netsalev3.bean.drive.Experience2
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.bean.drive.Reservemit
import com.cheyun.netsalev3.bean.login.MemberX
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.ApiStores
import com.cheyun.netsalev3.view.ChoiceDriveTimeActivity
import com.cheyun.netsalev3.view.drivelist.DriveListActivity
import com.cheyun.netsalev3.widget.DatePickerFragment
import com.dou361.dialogui.DialogUIUtils
import com.google.gson.Gson
import com.jeremyliao.liveeventbus.LiveEventBus


class AddDriveActivityViewModel(var editDriveParam: EditDriveParam, var addDriveActivityRepository: AddDriveActivityRepository) : BaseViewModel(), PopDialog.PopDialogListener {

    //选择的试驾日期
    var startDay = ObservableField<String>("")
    //选择的试驾时间
//    var startTime = ObservableField<String>("")
    var driveNum = MutableLiveData<String>("1")
    var startTime = MutableLiveData<String>("")
    var currentContext:Context? = null

    enum class Positin {
        cpzj,//试驾专员
        yylx,//预约类型
        sjlx,//试驾类型
    }

    var clickPositin: Positin? = null
    var yylslist = MutableLiveData<List<NameIdParam>>()
    //试驾类型
    var sjlxlist = MutableLiveData<List<NameIdParam>>()
    //产品专家
    var cpzjlist = MutableLiveData<List<NameIdParam>>()
    var baseDataParam: BaseDataParam? = MySharedPreferences.getBaseData()

    fun onClicShowPop(view: View, clickPositin: Positin) {
        this.clickPositin = clickPositin
        var content = view.context
        if (content is AppCompatActivity) {
            if (dialog == null) {
                dialog = PopDialog()
                dialog!!.listener = this
            }
            dialog?.show(content.supportFragmentManager, "pop")
        }
    }

    fun submit(view:View){
        if (editDriveParam.uname.isNullOrBlank()) {
            MyToast( "请输入姓名")
            return
        }
        if (editDriveParam.phone.isNullOrBlank()) {
            MyToast( "请输入手机号码")
            return
        }
        if (!FunctionUtils.isPhone2(editDriveParam.phone, view.context)) {
            MyToast( "请输入正确的手机号码")
            return
        }

        var dialog = DialogUIUtils.showLoadingVertical(view.context, "提交中...",false,false,false).show()
        var moth: (data: Any) -> Unit = {
            DialogUIUtils.dismiss(dialog)
            LiveEventBus.get().with("drivelistupdate").postValue("")
            myFinish()
        }
        var fail: () -> Unit = {
            DialogUIUtils.dismiss(dialog)
        }
        addDriveActivityRepository.editReservemit(editDriveParam,moth,fail)
    }

    init {
        addDriveActivityRepository.getDriveUsers(this::setDriveUser)
        baseDataParam?.apply {
            var temlist1 = ArrayList<NameIdParam>()
            if (this.drivetype != null) {
                for (item in this.drivetype) {
                    temlist1.add(NameIdParam(item.title, item.id.toString()))
                }
            }
            sjlxlist.postValue(temlist1)

            var temlist2 = ArrayList<NameIdParam>()
            if (this.reservetype != null) {
                for (item in this.reservetype) {
                    temlist2.add(NameIdParam(item.title, item.id.toString()))
                }
            }
            yylslist.postValue(temlist2)
        }
        if(editDriveParam.reserveid.isNotEmpty()){
            baseDataParam?.apply {
                if(editDriveParam.drivetype.isNotEmpty()){
                    if (this.drivetype != null) {
                        for (item in this.drivetype) {
                            if(item.id == editDriveParam.drivetype.toInt())
                                editDriveParam.drivetypename = item.title
                        }
                    }
                }
                if(editDriveParam.isreserve.isNotEmpty()){
                    if (this.reservetype != null) {
                        for (item in this.reservetype) {
                            if(item.id == editDriveParam.isreserve.toInt())
                                editDriveParam.isreservename = item.title
                        }
                    }
                }
            }
            startTime.postValue(editDriveParam.reservehalf)
            startDay.set(editDriveParam.reserveday)
        }
    }
    fun setDriveUser(list:List<MemberX>){
        var d = ArrayList<NameIdParam>()
        for(u in list){
            d.add(NameIdParam(u.realname,u.uid.toString()))
        }
        cpzjlist.postValue(d)
    }

    private var dialog: PopDialog? = null

    fun onChooseDateType(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val datePickerFragment = DatePickerFragment()
            datePickerFragment.clickOk = this::onDateSetStartDay
            datePickerFragment.show(content.supportFragmentManager, "datePicker")
        }
    }
    fun onDateSetStartDay(view: DatePicker, year: Int, month: Int, day: Int) {
        var d = "$year-$month-$day"
        startDay.set(d)
        editDriveParam.reserveday = d
        editDriveParam.reservehalf = ""
        editDriveParam.licence = ""
        startTime.postValue("")
    }

    fun onClicCar(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent(view.context, MarsChooseCarActivity::class.java)
            intent.putExtra("pos", 1)
            intent.putExtra("Isspec",true)
            intent.putExtra("drive",true)
            intent.putExtra("stime",startDay.get())
            var car = CarBrandParam("", editDriveParam.reserveseries, editDriveParam.reservespec,"")
            car.seriesname = editDriveParam.reserveseriesname
            car.specname = editDriveParam.reservespecname
            intent.putExtra("car", car as Serializable)
            content.startActivityForResult(intent, 101)
        }
    }
    fun onChooseTime(view: View) {
        if(editDriveParam.reservespec.isEmpty()){
            MyToast( "请先选择试驾车型")
            return
        }
        if(editDriveParam.reserveday.isEmpty()){
            MyToast( "请先选择试驾日期")
            return
        }
        var content = view.context
        if (content is AppCompatActivity) {
            var intent = Intent(view.context, ChoiceDriveTimeActivity::class.java)
//            intent.putExtra("editDriveParam",editDriveParam)
            intent.putExtra("reserveseries",editDriveParam.reserveseries)
            intent.putExtra("reservespec",editDriveParam.reservespec)
            intent.putExtra("reserveday",editDriveParam.reserveday)
            intent.putExtra("reserveseriesname",editDriveParam.reserveseriesname)
            intent.putExtra("reservespecname",editDriveParam.reservespecname)
            content.startActivityForResult(intent, 103)
        }
    }
    fun onAddDrivingnum(view:View,n:Int){
        if(n>0){
            editDriveParam.drivingnum = (editDriveParam.drivingnum.toInt()+1).toString()
        }else{
            if(editDriveParam.drivingnum.toInt()<=1)return
            editDriveParam.drivingnum = (editDriveParam.drivingnum.toInt()-1).toString()
        }
        driveNum.postValue(editDriveParam.drivingnum)
    }

    fun setCarData(carBrandParam: CarBrandParam) {
        editDriveParam.reserveseries = carBrandParam.series
        editDriveParam.reserveseriesname = carBrandParam.seriesname
        editDriveParam.reservespec = carBrandParam.spec
        editDriveParam.reservespecname = carBrandParam.specname
        editDriveParam.reservehalf = ""
        editDriveParam.licence = ""
    }

    fun setEditData(reserveday:String,
                    reserveseries:String,reserveseriesname:String,reservespec:String,reservespecname:String,
                    licence:String,reservehalf:String
    ) {
        editDriveParam.reserveday = reserveday
        editDriveParam.reserveseries = reserveseries
        editDriveParam.reserveseriesname = reserveseriesname
        editDriveParam.reservespec = reservespec
        editDriveParam.reservespecname = reservespecname
        editDriveParam.reservehalf = reservehalf
        editDriveParam.licence = licence

//        startDay.set(reserveday)
//        startTime.set(reservehalf)
    }
    fun setImage(img:String){
        editDriveParam.agreement = img
    }

    override fun onCleared() {
        super.onCleared()
        activity = null
    }

    var activity: AppCompatActivity? = null


    lateinit var mActivity:Activity
    fun setActivity(mContext:Activity){
        mActivity = mContext
    }

    //弹窗相关
    override var list: MutableLiveData<List<NameIdParam>>
        get() {
            when (clickPositin) {
                Positin.cpzj -> {
                    return cpzjlist
                }
                Positin.sjlx ->{
                    return sjlxlist
                }
                Positin.yylx ->{
                    return yylslist
                }
            }
            return MutableLiveData<List<NameIdParam>>()
        }
        set(value) {
            list = value
        }
    override var dialogType: MutableLiveData<String>
        get() {return MutableLiveData("list")}
        set(value) {}
    override var dialogTitle: MutableLiveData<String>
        get() {return MutableLiveData("请选择")}
        set(value) {}

    override fun onClickItem(view: View, param: NameIdParam) {
        when (clickPositin) {
            Positin.cpzj -> {
                cpzjlist.value?.apply {
                    for (item in this) {
                        item.IsSelect = param.id == item.id
                    }
                }
                editDriveParam.driveName = param.name
                editDriveParam.driveuid = param.id.toString()
            }
            Positin.sjlx -> {
                sjlxlist.value?.apply {
                    for (item in this) {
                        item.IsSelect = param.id == item.id
                    }
                }
                editDriveParam.drivetypename = param.name
                editDriveParam.drivetype = param.id.toString()
            }
            Positin.yylx -> {
                yylslist.value?.apply {
                    for (item in this) {
                        item.IsSelect = param.id == item.id
                    }
                }
                editDriveParam.isreservename = param.name
                editDriveParam.isreserve = param.id.toString()
            }
        }
        dialog?.dismiss()
    }

    override fun onDialogSubmit(view: View, param: String) {

    }
}